package com.beehoo.dcportal.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.CommentDao;
import com.beehoo.dcportal.admin.service.CommentService;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.CommentBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class CommentServiceImpl extends BaseService implements CommentService {
	
	private CommentDao commentDao;

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	@Override
	public void auditComment(CommentBean commentBean) throws BaseException {
		validateAudit(commentBean);
		int ret = commentDao.auditComment(commentBean);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.comment.audit.error");
		}
	}

	@Override
	public void delComment(String commentId) throws BaseException {
		if (CommonUtils.nvlToString(commentId).equals("")) {
			throw new BaseException("custom.messages.error.comment.id.empty");
		}
		int ret = commentDao.delComment(commentId);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.comment.del.error");
		}
	}

	@Override
	public CommentBean queryComment(String streetId, String commentId)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(commentId).equals("")) {
			throw new BaseException("custom.messages.error.comment.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("commentId", commentId);
		return commentDao.queryComment(paramMap);
	}

	@Override
	public List<CommentBean> queryComments(String streetId,
			String commentState, String beginDate, String endDate, int pageNo,
			int pageRows)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("commentState", commentState);
		paramMap.put("beginDate", beginDate + " 00:00:00");
		paramMap.put("endDate", endDate + " 23:59:59");
		paramMap.put("offset", (pageNo-1)*pageRows);
		paramMap.put("pageRows", pageRows);
		return commentDao.queryComments(paramMap);
	}
	

	@Override
	public int queryCommentCount(String streetId, String commentState,
			String beginDate, String endDate) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("commentState", commentState);
		paramMap.put("beginDate", beginDate + " 00:00:00");
		paramMap.put("endDate", endDate + " 23:59:59");
		return commentDao.queryCommentCount(paramMap);
	}
	
	private void validateAudit(CommentBean commentBean) throws BaseException {
		if (commentBean == null) {
			throw new BaseException("custom.messages.error.comment.audit.error");
		}
		if (CommonUtils.nvlToString(commentBean.getCommentState()).equals("")) {
			throw new BaseException("custom.messages.error.comment.state.empty");
		}
		if (CommonUtils.nvlToString(commentBean.getCommentId()).equals("")) {
			throw new BaseException("custom.messages.error.comment.id.empty");
		}
		if (CommonUtils.nvlToString(commentBean.getAuditorId()).equals("")) {
			throw new BaseException(
					"custom.messages.error.comment.auditor.empty");
		}
		if (CommonUtils.nvlToString(commentBean.getAuditor()).equals("")) {
			throw new BaseException(
					"custom.messages.error.comment.auditor.empty");
		}
	}

}
